const blinkDiff = require('blink-diff');

const fs = require('fs');
const path = require('path');
const readline = require('readline-sync');

//指定需要读取的表格根目录路径
let putDir = readline.question("dir:");

//指定表格打包之后的数据文件输出的路径
let outputPath = readline.question("output:");
let index = readline.question("index:");

const files = fs.readdirSync(putDir, "utf-8");
let pathArr = [];
files.forEach(file => {
    const filePath = path.join(putDir, file);
    const stat = fs.statSync(filePath);
    if (stat.isDirectory()) {
        // traverseFolder(filePath); // 递归遍历子文件夹
    } else {
        if (filePath.indexOf('.png') >= 0 && filePath.indexOf('$') == -1) {
            // console.log(filePath);
            let isDiff = true;
            for (let i = 0; i < pathArr.length; i++) {
                const img1 = fs.readFileSync(filePath);
                const img2 = fs.readFileSync(pathArr[i]);
                // 将图片解码为图像缓冲区
                const img1Buffer = Buffer.from(img1);
                const img2Buffer = Buffer.from(img2);
                const diff = new blinkDiff({
                    // imageAPath: path,
                    // imageBPath: pathArr[i],
                    imageA: img1Buffer,
                    imageB: img2Buffer,
                    isSameDimensions: true,
                    // imageOutputPath: 'path/to/output.png', // 可选，‌用于保存差异图片
                    // thresholdType: BlinkDiff.THRESHOLD_PERCENT, // 差异阈值类型
                    threshold: 0.00001 // 差异阈值，‌例如1%
                });
                let result = diff.runSync();
                if (result.differences / result.dimension == 0) {
                    isDiff = false
                    break
                }
            }
            if (isDiff) {
                pathArr.push(filePath);
            }
        }
    }
});


console.log(pathArr)
for (let i = 0; i < pathArr.length; i++) {
    fs.copyFile(pathArr[i], outputPath + '/' + (i + 1 + (+index)) + '.png', (err) => {
        if (err) throw err;
        console.log('文件已成功复制。');
    });
}